package ppp.ch14;

import java.util.Arrays;

/**
 * Bubble Sorter without pattern
 */
public class BubbleSorterPlain {
    static int operations = 0;

    public static int sort(int[] array) {
        operations = 0;

        if (array.length <= 1)
            return operations;

        for (int nextToLast = array.length - 2; nextToLast >= 0; nextToLast--)
            for (int index = 0; index <= nextToLast; index++)
                compareAndSwap(array, index);
        return operations;
    }

    private static void compareAndSwap(int[] array, int index) {
        if (array[index] > array[index + 1])
            swap(array, index);
        operations++;
    }

    private static void swap(int[] array, int index) {
        int temp = array[index];
        array[index] = array[index + 1];
        array[index + 1] = temp;
    }

    public static void main(String[] args) {
        int[] ints = { 8, 2, 4, 6, 1, 3, 10, 7, 5, 9 };
        final int operations = sort(ints);
        System.out.println("sorted ints " + Arrays.toString(ints) + " by " + operations + " operations.");
    }
}
